package com.edu.framework.websocket.handler;

import cn.hutool.core.util.StrUtil;
import com.edu.framework.websocket.WebSocketHolder;
import com.edu.framework.websocket.message.base.Response;
import com.edu.framework.websocket.message.scoreanalysis.ScoreAnalysisRequest;
import com.edu.framework.websocket.service.ScoreAnalysisService;
import com.edu.system.domain.vo.ScoreAnalysisVO;
import java.io.IOException;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

@Component
public class ScoreAnalysisRequestHandler implements IHandler<ScoreAnalysisRequest> {

    @Resource
    private ScoreAnalysisService wsScoreAnalysisService;

    @Override
    public void execute(WebSocketSession session, ScoreAnalysisRequest message) {
        if (message == null) {
            return;
        }
        Map<String, String> data = message.getData();
        String scoreId = data.get(ScoreAnalysisRequest.SCORE_ID);
        Response response = null;

        if (StrUtil.isBlank(scoreId)){
            response = Response.fail(ScoreAnalysisRequest.TYPE, "scoreId不能为空");
        } else {
            ScoreAnalysisVO vo = wsScoreAnalysisService.getAnalysisVOS(Long.parseLong(scoreId));
            response = Response.ok(ScoreAnalysisRequest.TYPE, ScoreAnalysisRequest.RESP_KEY, vo);
        }

        try {
            WebSocketHolder.send(session,response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String getType() {
        return ScoreAnalysisRequest.TYPE;
    }


}
